/*
 * Copyright 2018-2019 KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain menu_text_color copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wanchuang.hepos.ui.page.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.wanchuang.architecture.utils.InputMethodUtils;
import com.wanchuang.hepos.R;
import com.wanchuang.hepos.bridge.request.LoginRequestViewModel;
import com.wanchuang.hepos.bridge.state.login.MCCSelectViewModel;
import com.wanchuang.hepos.databinding.FragmentMccSelectBinding;
import com.wanchuang.hepos.ui.base.BaseFragment;
import com.wanchuang.hepos.ui.page.adapter.MccSelectAdapter;


/**
 * Create by KunMinX at 19/10/29
 */
public class MCCSelectFragment extends BaseFragment {

    private FragmentMccSelectBinding mBinding;
    private MCCSelectViewModel mViewModel;
    private LoginRequestViewModel mLoginRequestViewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(MCCSelectViewModel.class);
        mLoginRequestViewModel = ViewModelProviders.of(this).get(LoginRequestViewModel.class);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mcc_select, container, false);
        mBinding = FragmentMccSelectBinding.bind(view);
        mBinding.setClick(new ClickProxy());
        mViewModel.madapter.set(new MccSelectAdapter(mViewModel.datas));
        mViewModel.layoutManager.set(new LinearLayoutManager(getContext()));
        mBinding.setVm(mViewModel);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mViewModel.searchName.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if (!TextUtils.isEmpty(s))
                    mLoginRequestViewModel.getmcc(s);
            }
        });

        mLoginRequestViewModel.getErrorString().observe(this, this::showShortToast);  // 错误返回
        mLoginRequestViewModel.getMccArray().observe(this, mccs -> {
            mViewModel.madapter.get().replaceData(mccs);
            mViewModel.madapter.get().getLoadMoreModule().loadMoreEnd(true);
        });
    }


    public class ClickProxy {
        public void submit() {
        }

        public void back() {
            nav().navigateUp();
        }

        public void itemOnClick(int position) {
            Log.e("mango" , "itemOnClick: "+ mViewModel.datas.get(position).getMccName() );
            getSharedViewModel().mccUnPeekLiveData.setValue( mViewModel.datas.get(position));
            InputMethodUtils.hideSoftInput(mActivity);
            nav().navigateUp();
        }

        public void loadMoreListener() {

        }
    }
}
